﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EmailMarketingCampaign
{
    public partial class CampaignsList : System.Web.UI.Page
    {
        CampaignControl cc = new CampaignControl();
        MessageControl mc = new MessageControl();


        protected void Page_Load(object sender, EventArgs e)
        {

            if (GridView1.Rows.Count == 0)
            {
                Label1.Text = "No record";
                btnEditCampaign.Visible = false;
                Panel1.Visible = false;

            }
            else
            {


                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    int campaignID = Convert.ToInt16(GridView1.DataKeys[gvr.RowIndex].Value);

                    if (cc.getscheduleByCampaignID(campaignID).ToString() != "")
                    {
                        ((Button)gvr.Cells[4].FindControl("btnSchedule")).Text = "Click";
                        ((Button)gvr.Cells[4].FindControl("btnSchedule")).Enabled = true;
                    }

                    string schedule = cc.getscheduleByCampaignID(campaignID).ToString();


                    string[] ss = schedule.Split(',');

                    string sss = "";
                    for (int i = 0; i < ss.Count() - 1; i++)
                    {

                        sss = ss[i] + Environment.NewLine + sss;


                    }

                    ((TextBox)gvr.Cells[3].FindControl("txtSchedule")).Text = sss;
                    //((TextBox)gvr.Cells[3].FindControl("txtSchedule")).Style["overflow"] = "true";   

                }

            }
        }

        protected void btnAddCampaign_Click(object sender, EventArgs e)
        {
            Session["campaign"] = null;
            Session["message"] = null;
            Response.Redirect("CreateCamp.aspx");
            Panel1.Visible = false;
        }

        protected void btnEditCampaign_Click(object sender, EventArgs e)
        {
            bool select = false;
            foreach (GridViewRow gvr in GridView1.Rows)
            {

                if (((CheckBox)gvr.Cells[4].FindControl("select")).Checked == true)
                {
                    int campaignID = Convert.ToInt16(GridView1.DataKeys[gvr.RowIndex].Value);

                    select = true;
                    Campaign c = new Campaign();
                    Session["campaign"] = (Campaign)c;
                    c.setCampaignID(campaignID);
                    c.setCampaignName(gvr.Cells[1].Text.ToString());

                    int EmailID = cc.getEmailIDByCampaignID(campaignID);
                    c.setEmailID(EmailID);
                    int ThemeID = cc.getThemeIDByName(gvr.Cells[0].Text.ToString());
                    string From = cc.getFromByCampaignID(campaignID).ToString();
                    string To = cc.getToByCampaignID(campaignID).ToString();
                    string schedule = cc.getscheduleByCampaignID(campaignID).ToString();
                    c.setThemeID(ThemeID);
                    c.setDescription(gvr.Cells[2].Text.ToString());
                    c.setFrom(From);
                    c.setTo(To);
                    c.setSchedule(schedule);
                    c.setCOMUniquelID(Session["COMUniquelID"].ToString());


                    Message m = new Message();
                    Session["message"] = (Message)m;
                    string body = mc.getbodyByCampaignID(campaignID);
                    m.setBody(body);
                    string subject = mc.getSubjectByCampaignID(campaignID);
                    m.setSubject(subject);


                }
            }
            if (select)
            {
                btnEditCampaign.Visible = true;
                Response.Redirect("CreateCamp.aspx");
            }

            else
            {
              //  ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "aa", "alert('Please select at least one request.')", false); 

                this.Response.Write(" <script>alert('Please select at least one request.'); </script>");

            }
            Panel1.Visible = false;
        }





        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            foreach (GridViewRow gvr in GridView1.Rows)
            {

                int campaignID = Convert.ToInt16(GridView1.DataKeys[gvr.RowIndex].Value);
                string schedule = cc.getscheduleByCampaignID(campaignID).ToString();
                string[] ss = schedule.Split(',');
                string sss = "";

                for (int i = 0; i < ss.Count() - 1; i++)
                {

                    sss = ss[i] + Environment.NewLine + sss;


                }
                ((TextBox)gvr.Cells[3].FindControl("txtSchedule")).Text = sss;
            }

            this.Response.Write(" <script>alert('paging .'); </script>");
        }




        protected void GridView_RowDataBound1(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            }

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sss = "";
            if (e.CommandName == "schedule")
            {
             
                int index = Convert.ToInt32(e.CommandArgument);
                DataKey key = this.GridView1.DataKeys[index];
                int campaignID = Convert.ToInt32(key.Value);
                string schedule = cc.getscheduleByCampaignID(campaignID).ToString();


                string[] ss = schedule.Split(',');


                for (int i = 0; i < ss.Count() - 1; i++)
                {

                    sss = ss[i] + Environment.NewLine + sss;


                }


                TextBox1.Text = sss;
                Panel1.Visible = true;
               
            }

        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button Button1 = (Button)e.Row.FindControl("btnSchedule");
                Button1.CommandArgument = e.Row.RowIndex.ToString();
            }


        }

      
    
    
   
     


    }
}
